package com.uqbar.asteroids.components;

import java.awt.Color;

import com.uqbar.asteroids.scenes.AsteroidScene;
import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.GameComponent;
import com.uqbar.vainilla.appearances.Circle;
import com.uqbar.vainilla.events.constants.Key;

public abstract class PlayerShip extends GameComponent<AsteroidScene>{
	 private int speed;
	 private int live;
	 private int defense;
	 private int radio;
	 private int xMin, yMin=0;
	 private int xMax;
     private int yMax;
     private double angulo = 0; 
     private double angulopuro = 0;
   	 protected Vector2D velocidad= new Vector2D(0, 0);
 	 protected Vector2D velocidadAdicional= new Vector2D(0,0);
 	 protected Vector2D aceleracion = new Vector2D(speed,0);
 	 protected double limitador = 300;

	public Vector2D getAceleracion() {
		return aceleracion;
	}

	public void setAceleracion(Vector2D aceleracion) {
		this.aceleracion = aceleracion;
	}

	public Vector2D getVelocidad() {
		return velocidad;
	}

	public void setVelocidad(Vector2D velocidad) {
		this.velocidad = velocidad;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public int getLive() {
		return live;
	}

	public void setLive(int live) {
		this.live = live;
	}

	public int getDefense() {
		return defense;
	}

	public void setDefense(int defense) {
		this.defense = defense;
	}

	public int getRadio() {
		return radio;
	}

	public void setRadio(int radio) {
		this.radio = radio;
	}

	public int getxMin() {
		return xMin;
	}

	public void setxMin(int xMin) {
		this.xMin = xMin;
	}

	public int getyMin() {
		return yMin;
	}

	public void setyMin(int yMin) {
		this.yMin = yMin;
	}

	public int getxMax() {
		return xMax;
	}

	public void setxMax(int xMax) {
		this.xMax = xMax;
	}

	public int getyMax() {
		return yMax;
	}

	public void setyMax(int yMax) {
		this.yMax = yMax;
	}
	
	public void coordinarImagen(){
		  this.getScene().getSkinShip().setX(this.getX()-8);
		  this.getScene().getSkinShip().setY(this.getY()-8);
	}

	public PlayerShip(int speed, int live, int defense, int radio, double intiPosX, double initPosY){
    	 this.setAppearance(new Circle(Color.BLACK,radio));
    	 this.setY(initPosY);
    	 this.setX(intiPosX);
    	 this.live =live;
    	 this.defense = defense;
    	 this.radio = radio;
    	 this.speed = speed;
     }

	
	public void actualizarVelocidad(DeltaState deltaState) {

        if (deltaState.isKeyBeingHold(Key.W)) {
        	this.setVelocidad(this.getVelocidad().suma(this.aceleracion.toCartesians().producto(deltaState.getDelta())));
        }
        else            
            if (!deltaState.isKeyBeingHold(Key.W)) {
                this.setVelocidad(this.getVelocidad().suma(this.getVelocidad().producto(-deltaState.getDelta() * 0.5)));
            }
       
        if (deltaState.isKeyBeingHold(Key.D)) {
        	angulopuro = angulopuro + Math.PI * deltaState.getDelta();
        	if(angulopuro > 6.28){
        		angulopuro = 0;
        	}
            angulo = angulo + Math.PI * deltaState.getDelta();
            angulo = ajustarAnguloEntrePiYMenosPi(angulo);
            this.getScene().getSkinShip().rotar(angulopuro);

        } else if (deltaState.isKeyBeingHold(Key.A)) {
        	angulopuro = angulopuro - Math.PI * deltaState.getDelta();
        	if(angulopuro < 0){
        		angulopuro = 6.27;
        	}
            angulo = angulo - Math.PI * deltaState.getDelta();
            angulo = ajustarAnguloEntrePiYMenosPi(angulo);
            this.getScene().getSkinShip().rotar(angulopuro);
        }
       
        this.setAceleracion(new Vector2D(speed, angulo));
        
        if(deltaState.isKeyPressed(Key.UP) ){
        	if(this.limitador >= 300){
                this.shoot(new Vector2D(this.velocidad.toPolar().getX(),angulo));
                this.limitador = 0;
        	}
        }
        this.limitador+=this.limitador + deltaState.getDelta();
    }
   
    protected double ajustarAnguloEntrePiYMenosPi(double theta) {
        theta = theta > Math.PI ? theta - 2 * Math.PI : theta;
        theta = theta < -Math.PI ? theta + 2 * Math.PI : theta;
        return theta;
    }
   
    public void actualizarPosicion(double delta) {

        Vector2D posicionCartesiana = getPosicion().suma(this.velocidad.producto(delta));

        this.setX(posicionCartesiana.getX());
        this.setY(posicionCartesiana.getY());
    }
	
	public Vector2D getPosicion() {
		return new Vector2D(this.getX(), this.getY());
	}
	
	protected void reposicionar(){
		
		if(this.getY()>this.getGame().getDisplayHeight() + this.radio){
			this.setY( 0 - this.radio * 2);
		}
		
		if((this.getY()+this.radio*2)< (0)){
			this.setY( this.getGame().getDisplayHeight());
		}		
		
		if(this.getX()> this.getGame().getDisplayWidth() +this.radio){
			this.setX( 0 - this.radio*2 );
		}
		
		if((this.getX() + this.radio * 2) < 0 ){
			this.setX(this.getGame().getDisplayWidth() );
		}
	}
	
	public abstract void shoot(Vector2D orientacion);

}
